import { mapState, mapActions } from 'vuex'

export const auth = {
  methods: {
    ...mapActions({
      loginCheck: 'user/loginCheck'
    }),
  },
  computed: {
    ...mapState({
      isLogin: state => state.user.isLogin
    }),
  },
  beforeRouteEnter (to, from, next) {
    if (to.meta.auth) {
      next(async (vm) => {
        if (!vm.isLogin) {
          console.log(1)
          vm.$router.replace({ name: 'login' })
        } else {
          let logined = await vm.loginCheck()
          if (logined) {
            next()
          } else {
            vm.$router.replace({ name: 'login' })
          }
        }
      })
    } else {
      next()
    }
  }
}
